package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;

public class BibliotecaApp {

    private PrintStream printer;
    private BufferedReader reader;
    private Collection<Menu> userOptions;
    private Library library;

    BibliotecaApp(PrintStream printer, BufferedReader reader, Library library) {
        this.printer = printer;
        this.reader = reader;
        this.userOptions = Arrays.asList(new Menu("List of Books", 1), new Menu("Check Membership", 2));
        this.library = library;

    }

    public static void main(String[] args) throws IOException {
        BufferedReader bufferReader = new BufferedReader(new InputStreamReader(System.in));
        BibliotecaApp app = new BibliotecaApp(System.out, bufferReader, new Library(Arrays.asList(new Book("Harry Potter", 1)), System.out) ) ;
        app.printWelcome();
        app.go();
    }

    public void go() throws IOException {
        printOptions();
        respondToUserInput();
    }

    public void printWelcome() {
        printer.println("Welcome!");
    }

    public String getUserInput() throws IOException {
        return reader.readLine();
    }

    public void respondToUserInput() throws IOException {
        String userInput = getUserInput();
        if(userInput.equals("1")) {
            library.printListOfBooks();
            library.printBookListInstructions();
            library.reserveBook(getUserInput());
        }
        else if(userInput.equals("2")){
            printMembershipDetails();
        }
        else {
            printer.println("Select a valid option!!");
        }
    }

    public void printOptions() {
        String  userOptionDisplay = "";
        for(Menu option : userOptions)  {
            userOptionDisplay += (option.getMenuOptionName()+" "+option.getMenuOptionId()+" ");
        }
        printer.println(userOptionDisplay);
    }

    public void printMembershipDetails() {
        printer.println("Please talk to a Librarian. Thank you.");
    }
}
